﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UILibrary
{
    /// <summary>
    /// CircularBoard.xaml 的交互逻辑
    /// </summary>
    public partial class CircularBoard : UserControl
    {

        private List<CircularBoardItem> Items { get; set; } = new();
        public CircularBoard()
        {
            InitializeComponent();
            this.DataContext = this;

        }

        private void CreateChildren(int col)
        {
            Body.Children.Clear();
            var arcAngle = (double)(360 / (double)col);
            for (int i = 0; i < ColCount; i++)
            {
                var item = new CircularBoardItem();
                item.Margin = new Thickness() { Left = Width/2, Top = Height/2, Bottom = 0, Right = 0 };
                item.Angle = arcAngle;
                item.RadiusInner = RadiusInner;
                item.RadiusOuter = RadiusOuter;
                Body.Children.Add(item);
                item.Index = i;
                Items.Add(item);
            }

        }
        /// <summary>
        /// 分割栏数，默认12个
        /// </summary>
        public int ColCount
        {
            get { return (int)GetValue(ColCountProperty); }
            set {
                
                SetValue(ColCountProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for ColCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColCountProperty =
            DependencyProperty.Register("ColCount", typeof(int), typeof(CircularBoard), new PropertyMetadata(12, OnColCountChanged));
        private static void OnColCountChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            var element = o as CircularBoard;
            if (element != null)
            {
                //当值改变时，我们可以在此做一些逻辑处理
                element.CreateChildren((int)e.NewValue);
            }

        }

        /// <summary>
        /// 外半径
        /// </summary>
        public double RadiusOuter
        {
            get { return (double)GetValue(RadiusOuterProperty); }
            set
            {
                SetValue(RadiusOuterProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for RadiusOuter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusOuterProperty =
            DependencyProperty.Register("RadiusOuter", typeof(double), typeof(CircularBoard), new PropertyMetadata(100.0, PropertyChangedCallback));


        /// <summary>
        /// 内半径
        /// </summary>
        public double RadiusInner
        {
            get { return (double)GetValue(RadiusInnerProperty); }
            set
            {
                SetValue(RadiusInnerProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for RadiusInner.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusInnerProperty =
            DependencyProperty.Register("RadiusInner", typeof(double), typeof(CircularBoard), new PropertyMetadata(0.0, PropertyChangedCallback));


        private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as CircularBoard;
            if (element != null) { 
                foreach (var item in element.Items)
                {
                    item.RadiusInner = element.RadiusInner;
                    item.RadiusOuter = element.RadiusOuter;
                }
            }

        }
       

    }

}
